package client_package;

import javax.sound.sampled.*;
import client_package.Controleur.StatutSound;

public class ThreadSendRecord implements Runnable {
	private Controleur c;
	private AudioFormat format;
    
	public ThreadSendRecord(Controleur c ) {
		this.c = c;
		format = c.getAudioFormat();
	}
	
	public void run() {
		try {
			//System.out.println("Listening for outgoing sound");
	        DataLine.Info micInfo = new DataLine.Info(TargetDataLine.class, format);
	        TargetDataLine mic = (TargetDataLine) AudioSystem.getLine(micInfo);
	        mic.open(format);
	        byte tmpBuff[] = new byte[1024];
	        mic.start();
	        while(c.getStatutSound().equals(StatutSound.Enregistrement)) {
	            int count = mic.read(tmpBuff,0,tmpBuff.length);
	            if (count > 0){
	            	c.getOut_Sound().write(tmpBuff,0,count);
	            	c.getOut_Sound().flush();
	            }
	            
	        }
	        mic.drain();
	        mic.close();
	        //System.out.println("Stopped listening for outgoing sound");
	        
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}
}
